package com.atguigu04.example.exer4;

/**
 * ClassName: MyArray
 * Package: com.atguigu04.example.exer4
 * Description:
 *
 * @Author BuTang
 * @Create 2023/2/27 20:13
 * @Version 1.0
 */
public class MyArray {
    /**
     * 求最大值
     * @param arr
     * @return
     */
    public int getMax(int[] arr){
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= max) {
                max = arr[i];
            }
        }return max;
    }

    /**
     * 求最小值
     * @param arr
     * @return
     */
    public int getMin(int[] arr){
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] <= min) {
                min = arr[i];
            }
        }return min;
    }

    /**
     * 求和
     * @param arr
     * @return
     */
    public int getSum(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }return sum;
    }

    /**
     * 求平均值
     * @param arr
     * @return
     */
    public int getAvg(int[] arr){
        return getSum(arr)/arr.length;
    }

    /**
     * 遍历数组
     * @param arr
     */
    public void print(int[] arr){
        System.out.print("[");
        for (int i = 0; i < arr.length-2; i++) {
            System.out.print(arr[i]+",");
        }
        System.out.print(arr[arr.length-1]);
        System.out.println("]");
    }

    /**
     * 数组复制
     * @param arr
     * @return
     */
    public int[] copy(int[] arr){
        int[] temp = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            temp[i] = arr[i];
        }
        return temp;
    }

    /**
     * 数组的反转
     * @param arr
     */
    public void reverse(int[] arr){
        for (int i = 0; i < arr.length/2; i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length -1];
            arr[arr.length -1] = temp;
        }
    }

    /**
     * 冒泡排序
     * @param arr
     */
    public void sort(int[] arr){
        for (int i = 0; i < arr.length-1 ; i++) {
            boolean flag = true;
            for (int j = 0; j <arr.length-1 ; j++) {
                if (arr[j] >= arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j +1];
                    arr[j+1] = temp;
                    flag = false;
                }
                } if(flag == true){
                return;
            }

        }
    }

    /**
     * 查找
     * @param arr
     * @param target
     */
    public void linearSearch(int[] arr , int target){
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                System.out.println("找到了");
                flag = true;
            }
        }
        if (flag == false){
            System.out.println("没找到");
        }
    }



}
